package com.xiaoq.ar.repository.impl;

import com.mongodb.WriteConcern;
import com.xiaoq.ar.entity.MyTreasureEntity;
import com.xiaoq.ar.repository.MyTreasureRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.InsertOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2018/3/5.
 */
@Repository
public class MyTreasureRepositoryImpl implements MyTreasureRepository {

    @Autowired
    private Datastore dataStore;

    @Override
    public boolean save(MyTreasureEntity entity) {
        if(entity!=null && StringUtils.isNotBlank(entity.getId())){

            InsertOptions insertOptions = new InsertOptions();
            insertOptions.writeConcern(WriteConcern.W2);
            dataStore.save(entity, insertOptions);
            return true;
        }
        return false;
    }

    @Override
    public MyTreasureEntity getByArActivityUserId(String arActivityId, String userId) {
        if(StringUtils.isBlank(arActivityId) || StringUtils.isBlank(userId)){
            return null;
        }
        Query<MyTreasureEntity> query = dataStore.createQuery(MyTreasureEntity.class)
                .filter("arActivityId", arActivityId)
                .filter("userId", userId);

        return query.get();
    }

    @Override
    public boolean updateById(MyTreasureEntity myTreasureEntity) {
        if(myTreasureEntity==null || StringUtils.isBlank(myTreasureEntity.getId())){
            return false;
        }
        Query<MyTreasureEntity> query = dataStore.createQuery(MyTreasureEntity.class)
                .filter("_id", myTreasureEntity.getId());

        UpdateOperations<MyTreasureEntity> ups = dataStore.createUpdateOperations(MyTreasureEntity.class);
        ups.set("updatedAt", System.currentTimeMillis());

        if(CollectionUtils.isNotEmpty(myTreasureEntity.getPunchsList())){
            ups.set("punchsList", myTreasureEntity.getPunchsList());
        }

        if(myTreasureEntity.getExchange() != null){
            ups.set("exchange", myTreasureEntity.getExchange());
        }
        if(myTreasureEntity.getExchangeTime() != null){
            ups.set("exchangeTime", myTreasureEntity.getExchangeTime());
        }

        UpdateResults urs = dataStore.updateFirst(query, ups,false, WriteConcern.ACKNOWLEDGED);
        return urs.getUpdatedExisting();
    }

    @Override
    public List<MyTreasureEntity> getListByUserId(String userId) {
        if(StringUtils.isBlank(userId)){
            return new ArrayList<>();
        }
        Query<MyTreasureEntity> query = dataStore.createQuery(MyTreasureEntity.class)
                .filter("userId", userId);
        return query.asList();
    }
}
